package com.funo.logic.util;

import java.util.ArrayList;
import java.util.List;

/**
 * 下划线驼峰转化工具类
 */
public class HumpUnderLineUtil {

    /**
     * 下划线转驼峰命名
     */
    public static String underlineToCamel(String str) {
        StringBuilder result = new StringBuilder();
        String[] words = str.split("_");
        for (String word : words) {
            if (result.length() == 0) {
                result.append(word.toLowerCase());
            } else {
                result.append(word.substring(0, 1).toUpperCase());
                result.append(word.substring(1).toLowerCase());
            }
        }
        return result.toString();
    }

    /**
     * 将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空，则返回空字符串。 例如：HELLO_WORLD->HelloWorld
     *
     * @param name 转换前的下划线大写方式命名的字符串
     * @return 转换后的驼峰式命名的字符串
     */
    public static String convertToCamelCase(String name) {
        StringBuilder result = new StringBuilder();
        // 快速检查
        if (name == null || name.isEmpty()) {
            // 没必要转换
            return "";
        } else if (!name.contains("_")) {
            // 不含下划线，仅将首字母大写
            return name.substring(0, 1).toUpperCase() + name.substring(1);
        }
        // 用下划线将原始字符串分割
        String[] camels = name.split("_");
        for (String camel : camels) {
            // 跳过原始字符串中开头、结尾的下换线或双重下划线
            if (camel.isEmpty()) {
                continue;
            }
            // 首字母大写
            result.append(camel.substring(0, 1).toUpperCase());
            result.append(camel.substring(1).toLowerCase());
        }
        return result.toString();
    }

    /*
    下划线转横杠
     */
    public static String underlineToDash(String name) {
        // 快速检查
        if (name == null || name.isEmpty()) {
            // 没必要转换
            return "";
        } else if (!name.contains("_")) {
            // 不含下划线，仅将首字母大写
            return name.substring(0, 1).toUpperCase() + name.substring(1);
        }
        return name.replace("_","-");
    }



    /**
     * 驼峰转下划线
     * cD=> c_d
     * @param camelCaseString
     * @return
     */
    public static String camelCaseToUnderscore(String camelCaseString) {
        StringBuilder underscoreString = new StringBuilder();
        for (int i = 0; i < camelCaseString.length(); i++) {
            char currentChar = camelCaseString.charAt(i);
            if (Character.isUpperCase(currentChar)) {
                underscoreString.append("_");
                underscoreString.append(Character.toLowerCase(currentChar));
            } else {
                underscoreString.append(currentChar);
            }
        }
        return underscoreString.toString();
    }

    /**
     * 首字母大写
     * @param letters
     * @return
     */
    public static String upperCaseLetters(String letters){
        return Character.toUpperCase(letters.charAt(0)) + letters.substring(1);
    }

    /**
     * 首字母小写
     * @param letters
     * @return
     */
    public static String lowerCaseLetters(String letters){
        return Character.toLowerCase(letters.charAt(0)) + letters.substring(1);
    }


    public static List<String> extractCharContent(String str, Character left, Character right){
        String tem = "";
        List<String> results = new ArrayList<>();
        boolean flag = false;

        for (char c : str.toCharArray()) {
            if (c == left){
                tem = "";
                flag = true;
                continue;
            }else if (c == right){
                // 下划线转驼峰
                results.add(underlineToCamel(tem));
                flag = false;
                continue;
            }
            if (flag){
                tem += c;
            }
        }

        return results;
    }
}
